<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    Promise.race = function (promises) {
      return new Promise((resolve, reject) => {
        let len = promises.length;
        if (len === 0) return
        for (let i = 0; i < len; i++) {
          Promise.resolve(promises[i]).then(res => {
            resolve(res)
            return
          }).catch(err => {
            reject(err)
            return
          })
        }
      })
    }
    let p1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('ok')
      }, 3000)
    })
    let p2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        reject('err')
      }, 2000)
    })
    Promise.race([p1, p2]).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  </script>
</body>
</html>